from django.shortcuts import render, HttpResponse, redirect

from analysis import analysis
from user.models import *
import hashlib
from user.models import Position
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Map
from pyecharts.charts import Pie
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
from pyecharts.charts import HeatMap
from collections import Counter
from django.http import JsonResponse


# Create your views here.
def md5_password(password):  # 给密码进行md5加密的函数
    md5 = hashlib.md5()
    md5.update(password.encode())
    password = md5.hexdigest()
    return password


# 登录
def login(request):
    if request.method == "GET":
        if request.session.get("is_login"):
            return redirect('/user/index')
        return render(request, "login.html")
    else:
        username = request.POST.get("username")
        password = request.POST.get("password")

        password = md5_password(password)  # 先对密码进行加密再比较
        result = User.objects.filter(username=username, password=password)

        if result:

            request.session["username"] = username
            request.session["is_login"] = True
            # return redirect('/user/index/')
            res = {'result': 'true'}
        else:
            # 用户名或密码错误请重新输入！
            # return render(request, "login.html", {'result': 1})
            res = {'result': 'false'}
        return JsonResponse(res)


# 注册
def register(request):
    if request.method == "GET":
        return render(request, "register.html")
    else:
        username = request.POST.get("username")
        password = request.POST.get("password")
        tel = request.POST.get("tel")
        email = request.POST.get("email")
        address = request.POST.get("address")

        # 对比数据库内是否有相同的用户名
        result = User.objects.filter(username=username)
        # print(result)
        if result:
            # 用户名已存在，请修改
            res = {'result': 'false'}
            return JsonResponse(res)
            # return HttpResponse("用户名已存在请修改！")
        else:
            password = md5_password(password)  # 对用户密码进行加密
            User.objects.create(
                username=username,
                password=password,
                tel=tel,
                email=email,
                address=address
            )
            res = {'result': 'true'}
            return JsonResponse(res)
            # return redirect('/user/login/')


# 退出登录
def login_out(request):
    session_key = request.session.session_key
    request.session.delete(session_key)
    return redirect('/user/login/')


# 检查用户名是否存在
def check_name(request):
    if request.method == "POST":
        username = request.POST.get("username")
        result = User.objects.get(username=username)
        print(result)
        if result:  # 如果数据库中存在相同的用户名
            d = {'massage': '用户名已存在'}
            return JsonResponse({"data": d})  # 将信息传回客户端


def check_login(request):
    if request.session.get("is_login") is None:
        # 还没有登录
        return False
    return True


# 首页
def index(request):
    if not check_login(request):
        # 判断登录状态
        return redirect('/user/login/')
    username = request.session["username"]
    user = User.objects.get(username__exact=username)
    return render(request, "index.html")
